﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GestaltUtil;

namespace GestaltDesigner
{
    public partial class XamlEditor : UserControl
    {
        public XamlEditor()
        {
            InitializeComponent();
        }

        public void SerializeXaml(Canvas rootElement) 
        {
            string outputXaml = "<Canvas xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">\n";
            
            foreach (FrameworkElement element in rootElement.Children)
            {
                //skip placeholder rectangles
                if (element is Rectangle)
                    continue;
                outputXaml += "    <" + element.GetType().Name
                                  + " x:Name=\"" + element.Name + "\""
                                  + " Canvas.Left=\"" + element.GetValue(Canvas.LeftProperty).ToString() + "\""
                                  + " Canvas.Top=\"" + element.GetValue(Canvas.TopProperty).ToString() + "\""
                                  + " Width=\"" + element.Width.ToString() + "\""
                                  + " Height=\"" + element.Height.ToString() + "\""
                                  +" Opacity=\"" + element.Opacity.ToString() + "\"";

                if (element is ContentControl)
                {
                    outputXaml += " Content=\"" + (element as ContentControl).Content.ToString() + "\"";
                }
                if (element is Image)
                {
                    if ((element as Image).Source != null)
                        outputXaml += "\n Source=\"" + ((System.Windows.Media.Imaging.BitmapImage)(element as Image).Source).UriSource.OriginalString + "\"";
                }
                if (element is MediaElement)
                {
                    if ((element as MediaElement).Source != null)
                    {
                    outputXaml += "\n Source=\"" + (element as MediaElement).Source.OriginalString + "\"";
                    outputXaml += " AutoPlay=\"" + (element as MediaElement).AutoPlay.ToString() + "\"";
                    }
                }
                if (element is MultiScaleImage)
                {
                    if ((element as MultiScaleImage).Source != null)
                        outputXaml += "\n Source=\"" + ((DeepZoomImageTileSource)(element as MultiScaleImage).Source).UriSource.OriginalString + "\"";
                }

                outputXaml += "/>\n";
            }

            outputXaml += "</Canvas>";

            this.XamlEditBlock.Text = XmlUtil.XmlFullEndElements(outputXaml);
        }

        public string Xaml
        {
            get
            {
                return this.XamlEditBlock.Text;
            }
        }
    }
}
